package dao;

import model.Supplier;
import model.TradeType;
import model.Ware;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_supplier extends Dao{

    //获取所有供应商信息
    public static List selectAll()
    {
        List list = new ArrayList();
        String sql = "select * from supplier ";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Supplier supplier = new Supplier();

                supplier.setSupplierID(re.getString("s_no"));
                supplier.setSupplierName(re.getString("s_name"));
                supplier.setSupplierPeople(re.getString("s_people"));
                supplier.setSupplierPhone(re.getString("s_phone"));
                supplier.setAddress(re.getString("s_address"));

                list.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
    //添加
    public static int addSu(String s_id, String s_name, String phone, String people, String address)
    {
        int i = 0;
        try {
            String sql = "insert into supplier(s_no, s_name, s_phone, s_people, s_address) values('"+ s_id +"','"+ s_name +"','"+ phone +"','"+ people +"','" + address + "')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //修改
    public static int update(String s_id, String s_name, String phone, String people, String address)
    {
        int i = 0;

        try {
            String sql = "update  supplier set s_no = '" + s_id + "', s_name = '" + s_name + "', s_phone = '" + phone + "', s_people = '" + people + "', s_address = '"+ address + "' where s_no = '" + s_id + "'";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //删除
    public static int delete(String s_id)
    {
        int i = 0;
        try {
            String sql = "delete from  supplier where s_no = '" + s_id + "'";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //获取供应商
    public static Vector<String> getSupplier()
    {
        List list = Dao_supplier.selectAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Supplier supplier = (Supplier) list.get(i);
            WareID.add(supplier.getSupplierName());
        }

        return WareID;
    }

    //获取所有供应商编号
    public static Vector<String> getSupplierID()
    {
        List list = Dao_supplier.selectAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Supplier supplier = (Supplier) list.get(i);
            WareID.add(supplier.getSupplierID());
        }

        return WareID;
    }

    //通过供应商名称获取供应商信息
    public static List selectAll_name(String supplierName)
    {
        List list = new ArrayList();
        String sql = "select * from supplier where s_name ='" +  supplierName +"'";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Supplier supplier = new Supplier();

                supplier.setSupplierID(re.getString("s_no"));
                supplier.setSupplierPeople(re.getString("s_people"));
                supplier.setSupplierPhone(re.getString("s_phone"));
                supplier.setAddress(re.getString("s_address"));

                list.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
